<?php
/**
 * Created by PhpStorm.
 * User: johnny
 * Date: 17-9-12
 * Time: 下午10:00
 */

namespace api\controllers;


use common\tools\Logger;
use yii\rest\Controller;

class BaseAppController extends Controller
{
    public function behaviors()
    {
        return [];
    }


    /**
     * 格式化返回json数据
     * @param $data
     */
    protected function output($data)
    {
        header("Content-Type: application/json;charset=utf-8");
        $style = JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT;
        echo json_encode([
            'code' => 0,
            'data' => $data
        ],$style);
        Logger::debug('output',print_r($data));
        die;
    }

    /**
     * 无返回值,只返回成功状态
     */
    protected function outputSuccess(){

        header("Content-Type: application/json;charset=utf-8");
        $style = JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT;
        //$style = 0;
        $data =[
            'code'=>0,//ok 非0 错误码
            'msg'=>'ok',
        ];
        echo json_encode([
            'code' => 0,
            'data' => $data
        ],$style);
        die;
    }

    /**
     * 返回错误数据
     * @param $code
     * @param $msg
     */
    protected function error($code, $msg)
    {

        header("Content-Type: application/json;charset=utf-8");
        echo json_encode([
            'code' => $code,
            'msg' => $msg
        ]);
        die;
    }


}